package com.opc.Config;

import com.fazecast.jSerialComm.SerialPort;
import java.io.InputStream;
import java.util.Arrays;
import java.util.Properties;

public class jSerialCommIO {

    private String portName;
    private int baudRate;
    private int dataBits;
    private int stopBits;
    private int parity;

    public jSerialCommIO(String portName, int baudRate, int dataBits,
                         int stopBits, int parity) {
        this.portName = portName;
        this.baudRate = baudRate;
        this.dataBits = dataBits;
        this.stopBits = stopBits;
        this.parity = parity;
    }

    public boolean openPort(SerialPort serialPort) {
        System.out.println("baudRate是"+baudRate);
        serialPort.setBaudRate(baudRate);
        System.out.println("dataBits是"+dataBits);
        serialPort.setNumDataBits(dataBits);
        System.out.println("stopBits是"+stopBits);
        serialPort.setNumStopBits(stopBits);
        System.out.println("parity是"+parity);
        serialPort.setParity(parity);
        boolean y = serialPort.openPort();
        System.out.println("y是"+y);
        return serialPort.openPort();
    }

    public void closePort(SerialPort serialPort) {
        if (serialPort != null && serialPort.isOpen()) {
            serialPort.closePort();
        }
    }

    public void buzzerControl() {//String portName
        SerialPort serialPort = SerialPort.getCommPort(portName);
        System.out.println("连接串口："+portName);
        boolean x = openPort(serialPort);
        System.out.println("串口状态："+x);
        if (openPort(serialPort)) {
            System.out.println("串口连接成功！");
            try {
                // 发送高电平信号
//                byte[] buffer = new byte[]{(byte) 0xFF};
//                byte[] buffer = new byte[]{(byte) 0x00, (byte) 0x5A, (byte) 0x53, (byte) 0x00, (byte) 0x07,
//                        (byte) 0x00, (byte) 0x00, (byte) 0x00, (byte) 0xB4};
                //全吸合
//                byte[] buffer1 = new byte[]{0x00, 0x5A, 0x53, 0x00, 0x03, 0x00, 0x00, 0x00, (byte) 0xB0};
                //蜂鸣器响五秒自动断开00 5A 53 00 14 01 00 05 C7
//                byte[] buffer1 = new byte[] {
//                        (byte) 0x00, (byte) 0x5A, (byte) 0x53, (byte) 0x00, (byte) 0x14,
//                        (byte) 0x01, (byte) 0x00, (byte) 0x05, (byte) 0xC7
//                };
//                红灯亮00 5A 53 00 05 02 00 00 B4
                byte[] buffer1 = new byte[] {
                        (byte) 0x00, (byte) 0x5A, (byte) 0x53, (byte) 0x00, (byte) 0x05, (byte) 0x02, (byte) 0x00, (byte) 0x00, (byte) 0xB4
                };
                //红灯灭00 5A 53 00 06 02 00 00 B5
//                byte[] buffer1 = new byte[] {
//                        (byte) 0x00, (byte) 0x5A, (byte) 0x53, (byte) 0x00, (byte) 0x06, (byte) 0x02, (byte) 0x00, (byte) 0x00, (byte) 0xB5
//                };
                //绿灯亮00 5A 53 00 05 08 00 00 BA
//                byte[] buffer1 = new byte[] {
//                        (byte) 0x00, (byte) 0x5A, (byte) 0x53, (byte) 0x00, (byte) 0x05, (byte) 0x08, (byte) 0x00, (byte) 0x00, (byte) 0xBA
//                };
                //绿灯灭00 5A 53 00 06 08 00 00 BB
//                byte[] buffer1 = new byte[] {
//                        (byte) 0x00, (byte) 0x5A, (byte) 0x53, (byte) 0x00, (byte) 0x06, (byte) 0x08, (byte) 0x00, (byte) 0x00, (byte) 0xBB
//                };
                //黄灯亮00 5A 53 00 05 04 00 00 B6
//                byte[] buffer1 = new byte[] {
//                        (byte) 0x00, (byte) 0x5A, (byte) 0x53, (byte) 0x00, (byte) 0x05, (byte) 0x04, (byte) 0x00, (byte) 0x00, (byte) 0xB6
//                };
                //黄灯灭00 5A 53 00 06 04 00 00 B7
//                byte[] buffer1 = new byte[] {
//                        (byte) 0x00, (byte) 0x5A, (byte) 0x53, (byte) 0x00, (byte) 0x06, (byte) 0x04, (byte) 0x00, (byte) 0x00, (byte) 0xB7
//                };
                System.out.println("发送吸合信号"+ Arrays.toString(buffer1));
                serialPort.writeBytes(buffer1, buffer1.length);//1
                Thread.sleep(1000); // 蜂鸣器响1秒
                // 读取回复数据
                byte[] readBuffer1 = new byte[1024];
                int bytesRead1 = serialPort.readBytes(readBuffer1, readBuffer1.length);
                System.out.println("高电平信号回复数据："+ bytesRead1);
                if (bytesRead1 > 0) {
                    // 处理回复数据
                    System.out.println("高电平信号回复数据：" +
                            new String(readBuffer1, 0, bytesRead1));
                }


//                // 发送低电平信号
////                buffer[0] = 0x00;
//                byte[] buffer2 = new byte[] {
//                        (byte) 0x00, (byte) 0x5A, (byte) 0x53, (byte) 0x00, (byte) 0x08, (byte) 0x00, (byte) 0x00, (byte) 0x00, (byte) 0xB5
//                };
//                System.out.println("发送低电平信号"+ Arrays.toString(buffer2));
//                serialPort.writeBytes(buffer2, buffer2.length);//1
//                // 读取回复数据
//                byte[] readBuffer0 = new byte[1024];
//                int bytesRead0 = serialPort.readBytes(readBuffer0, readBuffer0.length);
//                System.out.println("低电平信号回复数据："+ bytesRead0);
//                if (bytesRead0 > 0) {
//                    // 处理回复数据
//                    System.out.println("低电平信号回复数据：" +
//                            new String(readBuffer0, 0, bytesRead0));
//                }
            } catch (InterruptedException e) {
                e.printStackTrace();
            } finally {
                closePort(serialPort);
            }
        } else {
            System.out.println("串口连接失败！");
        }
    }

    public static void main(String[] args) {
        Properties prop = new Properties();
        //调用以下静态方法生成系统上所有可用串行端口（真实或虚拟）的列表
        SerialPort[] serialPorts = SerialPort.getCommPorts();
        System.out.println("可用的串口："+ Arrays.toString(serialPorts));
        for (SerialPort serialPort : serialPorts) {
            System.out.println("串口名称：" + serialPort.getDescriptivePortName());
            System.out.println("串口路径：" + serialPort.getSystemPortPath());
        }
        try (InputStream input = jSerialCommIO.class.getClassLoader()
                .getResourceAsStream("config.properties")) {
            prop.load(input);
            jSerialCommIO io = new jSerialCommIO(
                    prop.getProperty("portName"),
                    Integer.parseInt(prop.getProperty("baudRate")),
                    Integer.parseInt(prop.getProperty("dataBits")),
                    Integer.parseInt(prop.getProperty("stopBits")),
                    Integer.parseInt(prop.getProperty("parity"))
            );
//            io.openPort(serialPorts[0]);
            io.buzzerControl();
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}


